import 'package:FineArt/constants.dart';
import 'package:FineArt/core/api/class_api.dart';
import 'package:FineArt/core/utils/toast.dart';
import 'package:FineArt/core/utils/utils.dart';
import 'package:FineArt/core/widget/list/notice_item.dart';
import 'package:FineArt/page/my/classdetail.dart';
import 'package:FineArt/page/my/createclass.dart';
import 'package:FineArt/utils/log_util.dart';
import 'package:FineArt/utils/size_config.dart';
import 'package:FineArt/utils/sputils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';

//选择学生
class SelectStudentPage extends StatefulWidget {
  ///0 添加 1 移除
  int type;
  TeacherClassesEntity classesEntity;
  SelectStudentPage(this.type,this.classesEntity);
  @override
  State<StatefulWidget> createState() {
    return _SelectStudentPageState();
  }
}

class _SelectStudentPageState extends State<SelectStudentPage> {
  List resources = [6];

  @override
  void initState() {
    super.initState();
    _loadClass();
  }

  List<ContactInfo> data = [];

  _loadClass() {
    String uid = SPUtils.getUID();
    int postId= (SPUtils.getRole() == "teacher")?1:2;
    int status= int.parse(widget.classesEntity.status);
    ClassApi().unSelectStudent(1,postId,1003).then((res) {
      if (res.code == 200) {
        setState(() {

        });
      } else {
        ToastUtils.toast("获取人员出错${res.msg}");
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
            title: Text(
              "选择联系人",
              style: TextStyle(color: text_black, fontSize: setSp(18)),
            ),
            centerTitle: true,
            elevation: 1,
            backgroundColor: Colors.white,
            brightness: Brightness.light,
            toolbarHeight: getPortraitSHeight(40),
            leading: IconButton(
                alignment: Alignment.centerLeft,
                icon: Image.asset(
                  "assets/images/icon_back_black.png",
                  width: getPortraitSWidth(11),
                  height: getPortraitSHeight(18),
                ),
                onPressed: () {
                  Navigator.of(context).pop();
                })),
        body: SafeArea(
            child: Stack(
              alignment: Alignment.center,
          children: [
            _widget_refresh_list(),
            Positioned(
              child: buildCreateClass(),
              bottom: 40,
            )
          ],
        )));
  }

  EasyRefreshController _controller = EasyRefreshController();

  Widget _widget_refresh_list() {
    double width = MediaQuery.of(context).size.width * 0.98;
    MaterialHeader header =
        MaterialHeader(completeDuration: new Duration(milliseconds: 100));
    return EasyRefresh.custom(
      controller: _controller,
      header: header,
      footer: MaterialFooter(),
      onRefresh: () async {},
      onLoad: () async {
        // _nextPage(widget.currentPath, widget.currentFilter);
      },
      slivers: <Widget>[
        // SliverToBoxAdapter(
        //   child: _playHeaderCol(isTeacher),
        // ),
        SliverPadding(
            padding: EdgeInsets.only(left: 3, right: 3),
            sliver: SliverList(
              delegate: SliverChildBuilderDelegate(
                (BuildContext context, int index) {
                  //创建子widget
                  // var resource = resources[index];
                  return buildClassItem(0, data[index].className, index);
                },
                childCount: data.length,
              ),
            )),

        //=====列表=====//
      ],
    );
  }

  Widget buildClassItem(int total, String name, int index) {
    return InkWell(
      child: Padding(
        padding: EdgeInsets.symmetric(
            vertical: getPortraitSHeight(12.5),
            horizontal: getPortraitSWidth(28)),
        child: Row(
          children: [
            Container(
              alignment: Alignment.center,
              decoration: BoxDecoration(
                color: Colors.redAccent,
                borderRadius: BorderRadius.all(Radius.circular(2.0)),
              ),
              child: Text(
                "班",
                style: TextStyle(fontSize: setSp(12), color: Colors.white),
              ),
              width: getPortraitSWidth(16),
              height: getPortraitSHeight(16),
            ),
            SizedBox(
              width: getPortraitSWidth(12.5),
            ),
            Text(
              name + (total>0?"($total)":""),
              style: TextStyle(fontSize: setSp(15), color: text_black),
            ),
            Expanded(child: Text("")),
            Image.asset(
              "assets/images/icon_right_arrow.png",
              width: getPortraitSWidth(8),
              height: getPortraitSHeight(16),
            ),
          ],
        ),
      ),
      onTap: () {
        LogUtil.d("class click $index");

      },
    );
  }

  Widget buildCreateClass() {
    return InkWell(
      onTap: () {
        Navigator.of(context).push(MaterialPageRoute(builder: (context){
          return CreateClassPage();
        }));
      },
      child: Container(
        width: SizeConfig.screenWidth*0.9,
        height: getPortraitSHeight(50),
        alignment: Alignment.center,
        decoration: BoxDecoration(
            color: red_main,
            borderRadius: BorderRadius.all(Radius.circular(25))),
        margin: EdgeInsets.symmetric(vertical: 0, horizontal: getPortraitSWidth(0)),
        child: Text(
          "创建班级",
          style: TextStyle(color: Colors.white, fontSize: 15),
        ),
      ),
    );
  }
}
